package com.qf.drbackend.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.drbackend.pojos.AddressBook;
import com.qf.drbackend.pojos.R;
import com.qf.drbackend.server.AddressBookService;
import com.qf.drbackend.utils.DataUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    @GetMapping("/default")
    public R defaultAddress(){
        Object uid = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        //查询当前用户的默认地址
        Wrapper<AddressBook> qw = Wrappers.lambdaQuery(AddressBook.class)
                .eq(AddressBook::getUserId,uid)
                .eq(AddressBook::getIsDefault,1);

        AddressBook one = addressBookService.getOne(qw);
        if (one == null){
            //如果没有默认地址，查询最近使用的地址
            qw = Wrappers.lambdaQuery(AddressBook.class)
                    .eq(AddressBook::getUserId,uid)
                    .orderByDesc(AddressBook::getUpdateTime);

            IPage<AddressBook> page = new Page(1,1);
            addressBookService.page(page,qw);
            if (page.getRecords() != null && page.getRecords().size() > 0){
                one = page.getRecords().get(0);
            }
        }

        //有编辑过地址
        if (one != null){
            //生成一个唯一标识
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");

            //存入后台(使用用户id作为键，每个用户之间数据互不影响)
            DataUtils.putData(uid.toString(),uuid);

            //将后台生成的唯一标识，携带到前台页面
            R<AddressBook> success = R.success(one);
            success.setMsg(uuid);
            return success;
        }

        //第一次使用，没有编辑过地址
        return R.error("没有地址信息");
    }

    @PostMapping
    public R addAddr(@RequestBody AddressBook addressBook){
        Object uid = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        addressBook.setUserId(Long.parseLong(uid.toString()));
        addressBook.setUpdateUser(Long.parseLong(uid.toString()));
        addressBook.setCreateUser(Long.parseLong(uid.toString()));

        addressBook.setCreateTime(new Date());
        addressBook.setUpdateTime(new Date());

        addressBookService.save(addressBook);

        return R.success("添加地址成功");
    }

    @GetMapping("/list")
    public R addAddr(){
        Object uid = SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        Wrapper<AddressBook> qw = Wrappers.lambdaQuery(AddressBook.class)
                .eq(AddressBook::getUserId,uid)
                .orderByDesc(AddressBook::getIsDefault, AddressBook::getUpdateTime);

        List<AddressBook> list = addressBookService.list(qw);

        return R.success(list);
    }

    @PutMapping("/default")
    public R setDefaultAddr(@RequestBody AddressBook addressBook){
        return addressBookService.setDefaultAddr(addressBook);
    }
}
